﻿using System.Net.Mail;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Configuration;
using TheCollectiveNetwork.Classes.Globals;

namespace ApplicationTemplate.Classes.MailTools
{
  
        public class Mailer
        {
            private ArrayList _tolist = new ArrayList(10);
            private string _subject;
            private string _body;
            private string _client;
            private string _fromaddress;
            private bool _mailerror = false;
            
            
            /* Constructors */
            public Mailer()
            {
                _client = appstring.maildomain;       
                _fromaddress = appstring.maildomainsender;                
            }

            /* Properties */
            public string subject
            {
                get { return _subject; }
                set { _subject = value; }
            }


            public string body
            {
                get { return _body; }
                set { _body = value; }
            }

            public bool mailerror
            {
                get { return _mailerror; }
            }


            /* Public Functions */
            public void Addrecipient(string recipient)
            {
                 if (recipient.Length > 0) {
                    _tolist.Add(recipient);
                 }
            }

            public void Send()
            {
                if (_tolist.Count > 0)
                { SendNotification(); }

            }


            /* Private Functions */
            private void SendNotification()
            {
                SmtpClient smtp;
                MailMessage mail;

                mail = new MailMessage();

                /* Is in try catch statement as will throw an error if mail server is not available.*/

                try
                {
                    mail.From = new MailAddress(_fromaddress);
                    foreach (object item in _tolist)
                    {
                        mail.To.Add(item.ToString());
                    }
                    mail.Subject = _subject;
                    mail.Body = _body;
                    smtp = new SmtpClient(_client);
                    smtp.Send(mail);
                    _mailerror = false;
                }
                catch
                { _mailerror = true; }


            }


        }


        public class AdminMailer : Mailer
        {

            public AdminMailer()
            {
                GetUserAlertList_Sql();
            }

            private void GetUserAlertList_Sql()
            {
                string rsql;
                SqlCommand sqlcmd;
                SqlConnection dbconn;
                SqlDataReader sqlreader;

                dbconn = new SqlConnection(ConfigurationManager.ConnectionStrings[constring.pub_str].ConnectionString);

                rsql = "Select username from t_access Where accesslevel = 1 and admin_alert = 1;";
                sqlcmd = new SqlCommand(rsql, dbconn);

                dbconn.Open();
                sqlreader = sqlcmd.ExecuteReader(CommandBehavior.CloseConnection);

                while (sqlreader.Read() == true)
                { Addrecipient(sqlreader["username"].ToString()); }

                sqlreader.Close();
                dbconn.Close();
            }


        }
    
}
